package org.etom.impl.ui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SaveAsDialog;
import org.etom.connector.datatypes.IMoodleModule;
import org.etom.impl.ui.Activator;
import org.etom.impl.ui.common.IconConstants;
import org.etom.impl.ui.jobs.CopyJob;
import org.etom.impl.ui.views.ViewsMessages;

/**
 * An action class which is responsible for the save
 * of the available module.
 */
public class SaveAsAction extends Action {

  private Shell shell;
  private IMoodleModule moodleModule;
  
  /**
   * Constructs the new save action.
   * 
   * @param shell - the shell is used in the wizard
   * @param moodleModule - the module which will be saved
   */
  public SaveAsAction(Shell shell, IMoodleModule moodleModule) {
    this.shell = shell;
    this.moodleModule = moodleModule;
    init();
  }
  
  /**
   * @see org.eclipse.jface.action.Action#run()
   */
  @Override
  public void run() {
    SaveAsDialog saveAsDialog = new SaveAsDialog(shell);
    saveAsDialog.setBlockOnOpen(true);
    saveAsDialog.setOriginalName(moodleModule.getReferenceName());
    if (SaveAsDialog.CANCEL == saveAsDialog.open()) return;
    CopyJob copyJob = new CopyJob(moodleModule, saveAsDialog.getResult());
    copyJob.setUser(true);
    copyJob.schedule();
  }
  
  /*
   * The method initializes the action.
   */
  private void init() {
    setText(ViewsMessages.Resources_Context_Menu_SaveAs);
    setImageDescriptor(ImageDescriptor.createFromFile(Activator.class,
        IconConstants.MENU_SAVEAS_ICON_PATH));
  }
  
}
